package cn.topevery.garden.wapi.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.constant.StringConstant;
import cn.topevery.garden.dal.logic.ProjBidLogic;
import cn.topevery.garden.dal.logic.ProjFilesLogic;
import cn.topevery.garden.entity.dto.ProjBidCudDTO;
import cn.topevery.garden.entity.dto.ProjBidDTO;
import cn.topevery.garden.entity.po.ProjFiles;
import cn.topevery.garden.entity.query.ProjBidQuery;
import cn.topevery.garden.entity.vo.ProjBidAndFilesVO;
import cn.topevery.garden.entity.vo.ProjBidAndProjNameVO;
import cn.topevery.garden.entity.vo.ProjFilesVO;
import cn.topevery.garden.framework.Database;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目招标表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Api(tags = "工程项目-项目招标表")
@RestController
@RequestMapping("/projBid")
public class ProjBidController extends BaseController {
    @Autowired
    ProjBidLogic projBidLogic;

    @Autowired
    ProjFilesLogic projFilesLogic;

    /**
     * 获取项目招标表分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取项目招标表分页接口")
    @PostMapping("/listPage")
    public JsonResult<DataPager<ProjBidAndProjNameVO>> listPage(@RequestBody ProjBidQuery query) {
        return JsonResult.ok(projBidLogic.listPage(query));
    }

    @ApiOperation(value = "获取项目招标表列表接口")
    @PostMapping("/list")
    public JsonResult<List<ProjBidAndProjNameVO>> list(@RequestBody ProjBidQuery query) {
        return JsonResult.ok(projBidLogic.list(query));
    }

    @ApiOperation("新增/编辑 项目招标表信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody List<ProjBidDTO> dtoList) {
        Database.transactionScope(() -> {
            projBidLogic.saveOrUpdate(getUser().userId, dtoList);
        });
        return JsonResult.ok(true);
    }

    @ApiOperation("新增/编辑/删除 项目招标表信息")
    @PostMapping("/saveOrUpdateOrDelete")
    public JsonResult<Boolean> saveOrUpdateOrDelete(@Valid @RequestBody ProjBidCudDTO cudDto) {
        Database.transactionScope(() -> {
            projBidLogic.saveOrUpdateOrDelete(getUser().userId, cudDto);
        });
        return JsonResult.ok(true);
    }

    @ApiOperation("根据guid获取详情和附件详情")
    @GetMapping("/getBidAndFilesById")
    public JsonResult<ProjBidAndFilesVO> getBidAndFilesById(@RequestParam(value = "guid") String guid) {
        return JsonResult.ok(projBidLogic.getBidAndFilesById(guid));
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        projBidLogic.deleteBatchById(getUser().userId,ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出项目招标表信息")
    @GetMapping("/export")
    public void export(ProjBidQuery query, HttpServletResponse response) {
        List<ProjBidAndProjNameVO> list = projBidLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "项目招标表", "sheet1", ProjBidAndProjNameVO.class, "项目招标表", response);
    }

}
